package online.lmds.sdmanage.main.upload_image.controller;


import online.lmds.sdmanage.common.Status;
import online.lmds.sdmanage.common.entity.Result;
import online.lmds.sdmanage.common.utils.FileNameUtils;
import online.lmds.sdmanage.common.utils.FilePathConfig;
import online.lmds.sdmanage.main.base.BaseContoller;
import online.lmds.sdmanage.main.base.UploadImage;
import online.lmds.sdmanage.main.upload_image.service.UploadImageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * 上传图片（文件）接口
 * @author Sunny  An
 *
 */
@Controller
public class UploadImageController extends BaseContoller {
    
    @Resource(name = "UploadImageService")
    private UploadImageService uploadImageService;
    
    
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public Object userUpload(@RequestParam(name = "file", required = false) MultipartFile file) {
        return upload(file, 1);
    }
    
    @RequestMapping(value = "/manage/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public Object adminUpload(@RequestParam(name = "file", required = false) MultipartFile file) {
        return upload(file, 2);
    }
    
    @RequestMapping(value = "/business/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public Object businessUpload(@RequestParam(name = "file", required = false) MultipartFile file) {
        return upload(file, 3);
    }
    
    
    /**
     * type = 1 user上传
     * type = 2 admin 上传
     * type = 3 business 上传
     */
    private Result upload(MultipartFile file, int type) {
        Result result = new Result();
        
        FilePathConfig.Type pathType;
        if (type == 1) {
            pathType = FilePathConfig.Type.USER;
        } else if (type == 2) {
            pathType = FilePathConfig.Type.ADMIN;
        } else {
            pathType = FilePathConfig.Type.BUSINESS;
        }
        
        if (file == null || file.isEmpty()) {
            return result.setErrorCode().setMessage("无效的文件大小").setStatus(Status.UPLOAD_NO_FILE);
        }
        if (!FileNameUtils.isFileType(file.getOriginalFilename(), FileNameUtils.FileType.IMAGE)) {
            return result.setErrorCode().setMessage("无效的文件类型").setStatus(Status.UPLOAD_FILE_TYPE_ERR);
        }
        String fileName = FileNameUtils.getUUID() + FileNameUtils.getFileType(file.getOriginalFilename());
        File saveFile = new File(FilePathConfig.getLocalPath(pathType, true), fileName);
        try {
            file.transferTo(saveFile);
        } catch (Exception e) {
            return result.setErrorCode().setMessage("保存文件失败").setStatus(Status.UPLOAD_SAVE_FAIL);
        }
        
        String url = FilePathConfig.getUrlPath(pathType) + fileName;
        UploadImage uploadImage = new UploadImage();
        uploadImage.setAdminId(null);
        uploadImage.setUserId(null);
        setUserInfo(uploadImage);

        uploadImage.setImgUrl(url);
        result = uploadImageService.save(uploadImage);
        return result;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

}
